
import $store from '@/store/index.js';

export default {
	
	openWindow(url,type='navigateTo'){//
		console.log('push url:'+url);
		switch (type){
			case 'navigateTo':uni.navigateTo({url: url,});break;
			case 'reLaunch':uni.reLaunch({url: url,});break;
			case 'redirectTo':uni.redirectTo({url: url,});break;
			case 'switchTab':uni.switchTab({url: url,});break;
			default:console.log('错误的跳转方式！');break;
		}
	},	
	
	back(tabType = 1){
		if(getCurrentPages() && getCurrentPages().length == 1){
			if(tabType == 1){
				this.openWindow('/pages/tabbar/index','switchTab');
			}
		}else{
			uni.navigateBack();
		}
	},
	
	toast(str, icon='none', time=2000){
		uni.showToast({
			title: str,
			icon: icon,
			duration: time,
		})
	},
	
	opener(){
		let pages = getCurrentPages();
		let opener = pages[pages.length - 2];
		// #ifdef H5
		return opener;
		// #endif
		return opener.$vm;
	},
	
	
	delEmpty(text){
		if(!text || text == 'undefined' || text == 'null'){
			return '';
		}
		return text;
	},
	
	delZero(text){
		if(!text || text == 'undefined' || text == 'null'){
			return '0';
		}
		return text;
	},
	
	toFix(value) {
		if (value) {
			let mValue = (parseInt(value * 1000) / 1000).toFixed(3);
			return (Math.round((mValue * 100).toFixed(2)) / 100).toFixed(2);
		}
		return "0.00";
	},
	
	os:{
		wechat:function(){
			// #ifdef H5
			var ua = window.navigator.userAgent.toLowerCase();
			if (ua.match(/MicroMessenger/i) == 'micromessenger') {
			    return true;
			}
			return false;
			// #endif
			return false;
		},
		h5:function(){
			// #ifdef H5
				return true;
			// #endif
			return false;
		},
		plus:function(){
			// #ifdef APP-PLUS
				return true;
			// #endif
			return false;
		},
		wxmp:function(){
			// #ifdef MP-WEIXIN
				return true;
			// #endif
			return false;
		},
		mp:function(){
			// #ifdef MP
				return true;
			// #endif
			return false;
		}
	},
	
	alert(message,confirm){
		uni.showModal({
			title:'温馨提示',
			content:message,
			showCancel:false,
			success:function(res){
				if (confirm) confirm();
			}
		})
	},			
	
	alertConfirm(message,confirm){
		uni.showModal({
			title:'温馨提示',
			content:message,
			success:function(res){
				if (res.confirm && confirm) {
					confirm();
				}
			}
		})
	},
	
	previewImage(current = 0,images = []){
		var option = ['保存图片'];
		uni.previewImage({
			current:current,
			urls: images,
			longPressActions: {
				itemList: option,
				success:(data) => {
					let image = urls[data.index]
					if(data.tapIndex == 0){
						this.saveImage(image)
					}
				}
			}
		});
	},
	saveImage(image){
		uni.saveImageToPhotosAlbum({
			filePath: image,
			success:(res) => {
				this.toast('保存成功!');
			}
		});
	},
}